package com.bocloud.cmp.web.controller.app.resource;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.web.controller.BasicController;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;

/**
 * 
 * @author zjm
 * @date 2017年3月17日
 */
@RestController
@RequestMapping("/application")
public class ApplicationController {
	private final String BASE_SERVICE = "/application";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private BasicController basicController;
	@Autowired
	private ServiceFactory serviceFactory;

	/**
	 * 创建应用
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.create(params, BASE_SERVICE, request, ApplicationController.class.getSimpleName());
	}

	/**
	 * 更新
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.modify(params, BASE_SERVICE, request, ApplicationController.class.getSimpleName());
	}

	/**
	 * 列表展示
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param simple
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROW, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
		return basicController.list(page, rows, params, sorter, simple, BASE_SERVICE, request,
				ApplicationController.class.getSimpleName());
	}

	@RequestMapping(value = "/listByLayout", method = { RequestMethod.POST })
	public BsmResult listByLayout(
			@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROW, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
		return basicController.list(page, rows, params, sorter, simple, BASE_SERVICE, request,
				ApplicationController.class.getSimpleName(), "/listByLayout");
	}

	/**
	 * 删除
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/remove", method = { RequestMethod.POST })
	public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String url = BASE_SERVICE + "/remove";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 详情
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	public BsmResult detail(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.detail(params, BASE_SERVICE, request, ApplicationController.class.getSimpleName());
	}

	/**
	 * 部署应用
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/layoutDeploy", method = { RequestMethod.POST })
	public BsmResult layoutDeploy(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String url = BASE_SERVICE + "/layoutDeploy";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			paramMap.put("appId", jsonObject.get("appId"));
			paramMap.put("layoutId", jsonObject.get("layoutId"));
			paramMap.put("clusterId", jsonObject.get("clusterId"));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 删除应用
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteLayoutDeploy", method = { RequestMethod.POST })
	public BsmResult deleteLayoutDeploy(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String url = BASE_SERVICE + "/deleteLayoutDeploy";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 变更rc实例数
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/scaleRc", method = { RequestMethod.POST })
	public BsmResult scaleRc(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String url = BASE_SERVICE + "/scaleRc";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 获取pod状态
	 * 
	 * @author zjm
	 * @date 2017年3月17日
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getPodStatus", method = { RequestMethod.POST })
	public BsmResult getPodStatus(@RequestParam(value = Common.PARAMS, required = true) String params,
			@RequestParam(value = "appId", required = true) Long appId,
			@RequestParam(value = "namespace", required = true) String namespace, HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String url = BASE_SERVICE + "/getPodStatus";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			paramMap.put("appId", appId);
			paramMap.put("namespace", namespace);
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 滚动升级
	 * 
	 * @author zjm
	 * @date 2017年3月25日
	 *
	 * @param params
	 * @param imageId
	 * @param appId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/rolling", method = { RequestMethod.POST })
	public BsmResult rolling(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "softwareId", required = true) Long softwareId,
			@RequestParam(value = "appId", required = true) Long appId,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceName", required = true) String resourceName, HttpServletRequest request) {
		String url = BASE_SERVICE + "/rolling";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("softwareId", softwareId);
		paramMap.put("appId", appId);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceName", resourceName);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}

	/**
	 * 回滚
	 * 
	 * @author zjm
	 * @date 2017年4月6日
	 *
	 * @param params
	 * @param imageName
	 * @param appId
	 * @param resourceName
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/rollback", method = { RequestMethod.POST })
	public BsmResult rollback(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) Long appId,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceName", required = true) String resourceName, HttpServletRequest request) {
		String url = BASE_SERVICE + "/rollback";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("appId", appId);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceName", resourceName);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}

	/**
	 * 滚动升级
	 * 
	 * @author zjm
	 * @date 2017年4月6日
	 *
	 * @param params
	 * @param appId
	 * @param resourceName
	 * @param requestValue
	 * @param minReplicas
	 * @param maxReplicas
	 * @param cpuTargetUtilization
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/autoScale", method = { RequestMethod.POST })
	public BsmResult autoScale(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) Long appId,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceName", required = true) String resourceName,
			@RequestParam(value = "requestValue", required = true) String requestValue,
			@RequestParam(value = "minReplicas", required = true) Integer minReplicas,
			@RequestParam(value = "maxReplicas", required = true) Integer maxReplicas,
			@RequestParam(value = "cpuTargetUtilization", required = true) Integer cpuTargetUtilization,
			HttpServletRequest request) {
		String url = BASE_SERVICE + "/autoScale";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("appId", appId);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceName", resourceName);
		paramMap.put("requestValue", requestValue);
		paramMap.put("minReplicas", minReplicas);
		paramMap.put("maxReplicas", maxReplicas);
		paramMap.put("cpuTargetUtilization", cpuTargetUtilization);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}

	/**
	 * 编辑rc资源
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editRcResource", method = { RequestMethod.POST })
	public BsmResult editRcResource(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		String url = BASE_SERVICE + "/editRcResource";
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}

	/**
	 * 获取组件pod资源
	 * 
	 * @author zjm
	 * @date 2017年4月13日
	 *
	 * @param params
	 * @param appId
	 * @param resourceName
	 * @param namespace
	 * @param resourceType
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getPodResource", method = { RequestMethod.POST })
	public BsmResult getPodResource(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) String appId,
			@RequestParam(value = "resourceName", required = true) String resourceName,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceType", required = true) String resourceType, HttpServletRequest request) {
		String url = BASE_SERVICE + "/getPodResource";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("resourceName", resourceName);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceType", resourceType);
		paramMap.put("appId", appId);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}

	/**
	 * 获取组件hpa资源
	 * 
	 * @author zjm
	 * @date 2017年4月13日
	 *
	 * @param params
	 * @param appId
	 * @param resourceName
	 * @param namespace
	 * @param resourceType
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getHPA", method = { RequestMethod.POST })
	public BsmResult getHPA(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) String appId,
			@RequestParam(value = "resourceName", required = true) String resourceName,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceType", required = true) String resourceType, HttpServletRequest request) {
		String url = BASE_SERVICE + "/getHPA";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("resourceName", resourceName);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceType", resourceType);
		paramMap.put("appId", appId);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}

	/**
	 * 删除资源
	 * 
	 * @author zjm
	 * @date 2017年4月6日
	 *
	 * @param params
	 * @param resourceName
	 * @param resourceType
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteResource", method = { RequestMethod.POST })
	public BsmResult deleteResource(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) String appId,
			@RequestParam(value = "resourceName", required = true) String resourceName,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceType", required = true) String resourceType, HttpServletRequest request) {
		String url = BASE_SERVICE + "/deleteResource";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("resourceName", resourceName);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceType", resourceType);
		paramMap.put("appId", appId);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}

	/**
	 * 获取资源实例数
	 * 
	 * @author zjm
	 * @date 2017年4月7日
	 *
	 * @param params
	 * @param resourceName
	 * @param resourceType
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getReplicas", method = { RequestMethod.POST })
	public BsmResult getReplicas(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) Long appId,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceName", required = true) String resourceName,
			@RequestParam(value = "resourceType", required = true) String resourceType, HttpServletRequest request) {
		String url = BASE_SERVICE + "/getReplicas";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("appId", appId);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceName", resourceName);
		paramMap.put("resourceType", resourceType);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}
	
	/**
	 * 江苏银行特殊需求：重建服务
	 * 
	 * @author zjm
	 * @date 2017年4月6日
	 *
	 * @param params
	 * @param imageName
	 * @param appId
	 * @param resourceName
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/rebuildServices", method = { RequestMethod.POST })
	public BsmResult rebuildServices(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "appId", required = true) Long appId,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceName", required = true) String resourceName, HttpServletRequest request) {
		String url = BASE_SERVICE + "/rebuildServices";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("appId", appId);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceName", resourceName);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}
	
	/**
	 * 全量升级
	 * 
	 * @author zjm
	 * @date 2017年12月5日
	 *
	 * @param params
	 * @param imageId
	 * @param appId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/upgrade", method = { RequestMethod.POST })
	public BsmResult upgrade(@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = "softwareId", required = true) Long softwareId,
			@RequestParam(value = "appId", required = true) Long appId,
			@RequestParam(value = "namespace", required = true) String namespace,
			@RequestParam(value = "resourceName", required = true) String resourceName, HttpServletRequest request) {
		String url = BASE_SERVICE + "/upgrade";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		paramMap.put("softwareId", softwareId);
		paramMap.put("appId", appId);
		paramMap.put("namespace", namespace);
		paramMap.put("resourceName", resourceName);
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap, request);
		return service.invoke();
	}
}
